package com.atstudy.resolver;

import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.Set;

/**
 * @version v1.0
 * @description:
 * @author: admin
 * @date: 2023/2/16 13:55
 * @copyright: 博为峰
 */
//@Component
//public class GolbalExceptionResolver implements HandlerExceptionResolver {
//    @Override
//    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
//        ModelAndView modelAndView = new ModelAndView();
//        modelAndView.addObject("msg", "处理中，稍后再试");
//        ex.printStackTrace();
//        modelAndView.setViewName("error");
//        return modelAndView;
//    }
//}

@ControllerAdvice
public class GolbalExceptionResolver {

    @ExceptionHandler(ArithmeticException.class)
    public String nullPointerException(Exception ex) {
        System.out.println("ArithmeticException");
        return "error";
    }

    @ExceptionHandler(ArrayIndexOutOfBoundsException.class)
    public String arrayIndexOutOfBoundsException(Exception ex) {
        System.out.println("ArrayIndexOutOfBoundsException");
        return "error";
    }

}